package com.rocksdata.mc.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.ctz.commonframe.ui.BaseActivity;
import com.ctz.commonframe.ui.BaseFragment;
import com.ctz.commonframe.ui.WebViewActivity;
import com.ctz.commonframe.ui.view.ptr.PullToRefreshBase;
import com.ctz.commonframe.ui.view.ptr.PullToRefreshListView;
import com.ctz.commonframe.ui.widget.AbsAdapter;
import com.ctz.commonframe.ui.widget.AdapterViewHolder;
import com.rocksdata.mc.business.BusinessInterface;
import com.rocksdata.mc.business.BusinessManager;
import com.rocksdata.mc.R;
import com.rocksdata.mc.business.UserInfoManager;
import com.rocksdata.mc.entity.OAData;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 陈堂正 on 2017/5/18.
 */
@ContentView(R.layout.fragment_oa_treat)
public class TreatedOAFragment extends BaseFragment {
    @ViewInject(R.id.lv_oa_treat)
    private PullToRefreshListView lvOATreat;

    private List<OAData> oaBaseInfoList = new ArrayList<>();
    private OAListAdapter adapter;
    private BusinessInterface.IOAManager manager;
    private int index;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        manager = BusinessManager.getOAManager();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        lvOATreat.setMode(PullToRefreshBase.Mode.BOTH);
        adapter = new OAListAdapter(getActivity(), oaBaseInfoList);
        lvOATreat.setAdapter(adapter);
        lvOATreat.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                refresh();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                loadMore();
            }
        });
//        lvOATreat.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                OAData info = adapter.getItem(position - 1);
//                if(!TextUtils.isEmpty(info.getTaskPage())) {
//                    Intent intent = new Intent(mContext, WebViewActivity.class);
//                    intent.putExtra(WebViewActivity.EXTRA_KEY_URL, info.getTaskPage()
//                            + "&uid=" + UserInfoManager.getCurrentUserId()
//                            + "&pwd=" + UserInfoManager.getOAPassword());
//                    startActivity(intent);
//                }
//            }
//        });
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    protected void onVisible() {
        if (oaBaseInfoList.size() == 0) {
            ((BaseActivity)mContext).showLoadingDialog();
        }
        refresh();
    }

    private void refresh(){
        index = 1;
        oaBaseInfoList.clear();
        getTreatedOAInfoList();
    }

    private void loadMore(){
        index++;
        getTreatedOAInfoList();
    }

    private void getTreatedOAInfoList(){
        manager.getTreatedOAList(index, new BusinessInterface.OnBusinessResultListener() {
            @Override
            public void onBusinessResult(boolean isSuccess, Object object, String message) {
                if(isSuccess){
                    @SuppressWarnings("unchecked")
                    List<OAData> list = (List<OAData>) object;
                    if(list != null && list.size() > 0){
                        for(OAData info: list){
                            if(!oaBaseInfoList.contains(info)){
                                if("客饭申请v2".equals(info.getFlowName())
                                        || "用车申请v2".equals(info.getFlowName())
                                        || "请假申请v2".equals(info.getFlowName())
                                        || "故障报修申请v1".equals(info.getFlowName())
                                        || "出差申請".equals(info.getFlowName())){
                                    oaBaseInfoList.add(info);
                                }
                            }
                        }
                    }
                }else{
                    if(index > 1)
                        index--;
                }
                adapter.notifyDataSetChanged();
                lvOATreat.onRefreshComplete();
                ((BaseActivity)mContext).dismissLoadingDialog();
            }
        });
    }


    private class OAListAdapter extends AbsAdapter<OAData>{

        private OAListAdapter(Context context, List<OAData> datas) {
            super(context, datas, R.layout.item_oa_info_list);
        }

        @Override
        public void convert(AdapterViewHolder holder, OAData data) {
            TextView tvType = holder.getView(R.id.tv_oa_type);
            TextView tvProposer = holder.getView(R.id.tv_proposer);
            TextView tvDate = holder.getView(R.id.tv_propose_date);
            TextView tvResult = holder.getView(R.id.tv_result);

            tvType.setText(data.getFlowName());
            tvProposer.setText(data.getCreateUserName());
            tvDate.setText(data.getCreateTime());
            tvResult.setText(data.getTaskName());
        }
    }

}
